<?php

declare(strict_types=1);

namespace app\shop\controller\admin\decorate;

use think\Request;
use think\Response;
use think\facade\Db;
use sheep\controller\Backend;
use sheep\exception\SheepException;
use app\shop\model\decorate\Decorate as DecorateModel;
use app\shop\model\decorate\Page as PageModel;
use sheep\controller\traits\Crud;

class Template extends Backend
{
    use Crud;
    protected $model = null;
    protected $isPaginate = true;
    protected $onlyParams = ['type', 'name', 'memo', 'platform'];

    public function initialize()
    {
        $this->model = new DecorateModel();
    }

    /**
     * 查看
     *
     * @return Response
     */
    public function list(Request $request): Response
    {
        $list = $this->model->sheepFilter()->with(['page' => function ($query) {
            return $query->where('type', 'in', ['home', 'user', 'diypage'])->field('decorate_id, type, image');
        }])->select();
        return success('获取成功', $list);
    }

    /**
     * 复制
     *
     * @param  $id
     * @return \think\Response
     */
    public function copy($id)
    {
        $template = $this->model->findOrFail($id);

        Db::transaction(function () use ($template) {
            $params = [
                'name' => '复制 ' . $template->name,
                'type' => $template->type,
                'memo' => $template->memo,
                'platform' => $template->platform,
                'status' => 'disabled'
            ];
            $newTemplate = $this->model->create($params);

            $pageList = Db::name('shop_decorate_page')->where('decorate_id', $template->id)->select();

            $newPageList = [];
            foreach ($pageList as $page) {
                $newPageList[] = [
                    'decorate_id' => $newTemplate->id,
                    'type' => $page['type'],
                    'page' => $page['page'],
                    'image' => $page['image']
                ];
            }
            if (count($newPageList) > 0) {
                PageModel::insertAll($newPageList);
            }
        });

        return success('复制成功');
    }

    /**
     * 启用/禁用
     *
     * @param  $id
     * @return \think\Response
     */
    public function status(Request $request, $id)
    {
        $status = $request->param('status');
        $template = $this->model->findOrFail($id);
        $template->status = $status;
        $template->save();
        return success('操作成功', $template);
    }

    /**
     * 销毁(支持批量)
     *
     * @param  $id
     * @return \think\Response
     */
    public function destroy($id)
    {
        $result = Db::transaction(function () use ($id) {
            $count = 0;
            if ($id !== 'all') {
                $model = $this->model->onlyTrashed()->whereIn('id', $id);
            } else {
                $model = $this->model->onlyTrashed();
            }

            foreach ($model->cursor() as $row) {
                PageModel::where('decorate_id', $row->id)->delete();
                $count += $row->force()->delete();
            }
            return $count;
        });

        if ($result) {
            return success('销毁成功', $result);
        }
        return error('销毁失败');
    }

    /**
     * 选择自定义页面
     *
     * @return Response
     */
    public function select(Request $request): Response
    {
        $list = $this->model->where('type', 'diypage')->with(['page' => function ($query) {
            return $query->field('decorate_id, type, image');
        }])->select();
        return success('获取成功', $list);
    }
}
